<template>
  <div>
    <div class="login-box">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="60px"
        class="demo-ruleForm"
      >
        <div class="title">
          <h3>三加一理科后台</h3>
        </div>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="ruleForm.email"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="ruleForm.password" show-password></el-input>
        </el-form-item>
        <el-form-item label="部门" prop="role">
          <el-select v-model="ruleForm.role" placeholder="请选择您的部门">
            <el-option label="教学部" value="teacher">教学部</el-option>
            <el-option label="人事部" value="renshi">人事部</el-option>
            <el-option label="财务部" value="caiwu">财务部</el-option>
            <el-option label="学管部" value="xueguan">学管部</el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="canlogin">立即登录</el-button>
          <el-button type="primary" style="margin-left:40px">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    var checkEmail = (rule, value, callback) => {
      const mailReg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/;
      if (!value) {
        return callback(new Error("邮箱不能为空"));
      }
      setTimeout(() => {
        if (mailReg.test(value)) {
          callback();
        } else {
          callback(new Error("请输入正确的邮箱格式"));
        }
      }, 100);
    };
    return {
      ruleForm: {
        email: "12345@qq.com",
        password: "123456",
        role: "教学部",
      },
      rules: {
        email: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
          {
            validator: checkEmail,
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入用户密码", trigger: "blur" },
          {
            min: 6,
            max: 15,
            message: "长度在 6 到 15 个字符",
            trigger: "blur",
          },
        ],
        role: [
          { required: true, message: "请选择你的部门", trigger: "change" },
        ],
      },
    };
  },
  methods: {
    canlogin() {
      this.$refs.ruleForm.validate((valid) => {
        if (!valid) {
          return this.$message({
            message: "您的输入有误！！",
            type: "error",
          });
        }
        console.log(5555);
        this.$http
          .post("api/login", this.ruleForm)
          .then((result) => {
            console.log(result);
          })
          .catch((err) => {
            console.log(err);
          });
      });
    },
  },
};
</script>

<style lang="less" scoped>
.login-box {
  position: relative;
  overflow: hidden;
  width: 100%;
  height: 100vh;
  background: #52d0ff;
  .title {
    width: 100%;
    height: 40px;
    font-size: 24px;
    color: rgb(109, 109, 109);
    text-align: center;
    line-height: 40px;
    margin-bottom: 30px;
  }
  .el-form {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    box-sizing: border-box;
    padding: 0px 50px;
    border-radius: 20px;
    width: 30%;
    height: 50vh;
    background-color: #fff;
  }
}
</style>